﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.SalesOrderCommand;

namespace SmartProduction.Write.API.Application.CommandHandlers.SalesOrderHandler
{
    /// <summary>
    /// 销售订单修改命令处理
    /// </summary>
    public class SalesOrderUpdCommandHandler : IRequestHandler<SalesOrderUpdCommand, APIResult<object>>
    {
        /// <summary>
        /// 销售订单管理仓储
        /// </summary>
        private readonly IBaseRepository<SalesOrder> salesRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="salesRepository">销售订单仓储</param>
        /// <param name="mapper">映射</param>
        public SalesOrderUpdCommandHandler(IBaseRepository<SalesOrder> salesRepository,IMapper mapper)
        {
            this.salesRepository = salesRepository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<APIResult<object>> Handle(SalesOrderUpdCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            //查询修改的model
           var obj=mapper.Map<SalesOrder>(request);

            //obj.CountryName = request.CountryName;
            //obj.ClientInfoNumer = request.ClientInfoNumer;
            //obj.SalesOrderNumer = request.SalesOrderNumer;
            //obj.SalesOrderPrice = request.SalesOrderPrice;
            //obj.SalesOrderRemark = request.SalesOrderRemark;
            //obj.SalesMan = request.SalesMan;
            //obj.SalesDepart = request.SalesDepart;
            //obj.Currency = request.Currency;
            //obj.ClientInfoNo = request.ClientInfoNo;
            //obj.Advances = request.Advances;
            //obj.OrderCreatTime = request.OrderCreatTime;
            //obj.OrderOverTime = request.OrderOverTime;
            //obj.AltEnter = request.AltEnter;
            //obj.SigningTime = request.SigningTime;
            //obj.InvoicePrice = request.InvoicePrice;
            //obj.AmountPrice = request.AmountPrice;
            //obj.Taxrate = request.Taxrate;
            //obj.TaxType = request.TaxType;

            //修改
            int res = salesRepository.UpdateAsync(obj).Result;

            if (res > 0) 
            {
                result.Code = APIEnums.修改成功;
                result.Msg = APIEnums.修改成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnums.修改失败;
                result.Msg = APIEnums.修改失败.ToString();
                result.data = res;
            }
         
            return await Task.FromResult(result);
        }
    }
}
